var $, layer, laydate, table, form;
var URL_HOST = setWinHost().URL_HOST,
  TIP_TIME = setWinHost().TIP_TIME,
  date = dayjs().format("YYYY-MM-DD");
var API = {
  transmit: "ts/hello", // 验证transmit状态
  getConfig: "json/cache", // 加载配置文件数据
  setConfig: "json/update", // 更新配置文件数据
  getFile: "", // 获取文件列表
  downFile: "", // 下载单文件
};
layui.use(["element", "laydate", "table", "form"], function () {
  ($ = layui.$),
    (element = layui.element),
    (layer = layui.layer),
    (laydate = layui.laydate),
    (table = layui.table),
    (form = layui.form);
  // 监听验证transmit状态
  form.on("submit(searForm)", function (data) {
    getTransmit(
      data.field.host +
        "://" +
        data.field.ip +
        ":" +
        data.field.port +
        "" +
        data.field.classPath
    );
    return false;
  });
  // 监听tab切换
  element.on("tab(win_tabs)", function (data) {
    switch (data.index) {
      case 0:
        break;
      case 1:
        laydate.render({
          elem: "#date",
          type: "date",
          format: "yyyy-MM-dd",
          value: date,
          theme: "#2D5AFA",
          btns: ["now", "confirm"],
          showBottom: true,
          done: function (value) {
            date = value;
            getFileTable();
          },
        });
        getFileTable();
        break;
    }
  });
  // 监听下拉选择切换
  form.on("select(jsonFilter)", function (data) {
    getConfig();
  });
});

// 验证transmit状态
function getTransmit(url) {
  $("#win_config,#win_tag").html("").hide();
  // url = "http://60.173.233.110:1880/windhptransmit/hello"; // 测试
  let config = {
    type: "GET",
    url: URL_HOST + API.transmit + "?url=" + url + "hello",
    contentType: "application/x-www-form-urlencoded;charset=UTF-8",
    dataType: "json",
    success: function (res) {
      if (res.code == 0) {
        let lists = [
          { title: "前置机IP", content: res.data.hostIp },
          { title: "前置机端口", content: res.data.port },
          { title: "部署路径", content: res.data.classPath },
        ];
        let html = "";
        lists.forEach((item) => {
          html += `<li><span>${item.title}</span><span>${item.content}</span></li>`;
        });
        $("#win_config").html(html).show();
      } else {
        $("#win_tag").html("数据获取失败：HTTP错误500").show();
      }
    },
    error: function (xhr, status, error) {
      $("#win_tag").html("数据获取失败: HTTP错误 500").show();
    },
  };
  $.ajax(config);
}

// 加载配置文件数据
function getConfig() {
  // let config = {
  //   type: "GET",
  //   url: URL_HOST + API.getConfig + "?type=" + $("#json").val().trim(),
  //   contentType: "application/x-www-form-urlencoded;charset=UTF-8",
  //   dataType: "json",
  //   success: function (res) {
  console.log("获取之后设置JSON回显");
  let jsonRes = {
    code: 0,
    data: {
      hostIp: "192.168.20.20",
      port: "9527",
      classPath: "D:\\data\\jar\\transmit",
    },
  }; // 测试
  let jsonHtml = `<div id="Canvas" class="Canvas"></div>
      <textarea style="display:none;" class="detail-info" id="RawJson">${JSON.stringify(
        jsonRes
      )}</textarea>`;
  $("#jsonHtml").html(jsonHtml);
  Process("Canvas", "RawJson");
  //   },
  // };
  // $.ajax(config);
}

// 更新配置文件数据
function setConfig() {
  let cont = $("#self").val().trim();
  if (cont === "") {
    layer.msg("请先输入更新内容！", {
      icon: 0,
      time: TIP_TIME,
    });
  } else {
    console.log(cont);
    let config = {
      type: "POST",
      url: URL_HOST + API.setConfig,
      data: { cont: cont },
      contentType: "application/x-www-form-urlencoded;charset=UTF-8",
      dataType: "json",
      success: function (res) {
        console.log("更新配置成功后操作");
      },
    };
    // $.ajax(config);
  }
}

// 获取文件表格数据
function getFileTable() {
  let tableData = [],
    count = 0;
  let tableOptions = {
    elem: "#table_file",
    id: "layTable_file",
    height: 500,
    data: [

    ], // 测试
    url: API.getFile + "?url=" + "http://127.0.0.1:1234/log/list?data=" + date,
    method: "GET",
    request: { pageName: "pageNum", limitName: "pageSize" },
    parseData: function (res) {
      if (res.code === 0) {
        (tableData = res.rows), (count = res.total);
      } else {
        (tableData = []), (count = 0);
      }
      return { code: "", msg: "", count: count, data: tableData };
    },
    cellMinWidth: 100,
    totalRow: false,
    cols: [
      [
        { field: "id", width: 60, title: "序号" },
        { field: "name", title: "文件名" },
        { field: "updateTime", title: "更新时间", width: 170 },
        { field: "size", title: "文件大小" },
        {
          field: "",
          title: "操作",
          width: 110,
          fixed: "right",
          templet: function () {
            return `<span class="win_button_text" lay-event="down" onclick="setDown()">下载</span>`;
          },
        },
      ],
    ],
    toolbar: false,
    defaultToolbar: [],
    loading: false,
    limit: 100000,
    page: false,
    text: {
      none: "暂无相关数据",
    },
    done: function (res) {
      layer.closeAll("loading");
    },
  };
  let wait = function () {
    table.init("table_file_id", tableOptions);
  };
  wait();
}

// 下载
function setDown() {
  table.on("tool(table_file_id)", function (obj) {
    if (obj.event === "down") {
      let json = obj.data;
      let config = {
        type: "POST",
        url: URL_HOST + API.downFile,
        data: { id: json.id },
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        dataType: "json",
        success: function (res) {
          console.log("下载成功后操作");
        },
      };
      // $.ajax(config);
    }
  });
}

// 设置请求路径
function setWinHost() {
  let CONSTANTS = {
    "v.test": "http://127.0.0.1:1234/",
    "v.dev": "https://windhp.com/",
  };
  let CUR_HOST = CONSTANTS["v.test"];
  CUR_HOST = ""; // 本地调试
  return {
    URL_HOST: CUR_HOST,
    TIP_TIME: 1500,
  };
}
